<script setup>
import { reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import usage from './usage.vue'
import { getUsageList,delUsageApi } from '@/api/usage.js'
//时间戳转时间
import dayjs from 'dayjs'
import { communityListApi, carUseStatus } from '@/hooks'
const community = communityListApi()
//使用性质(租 售)
const carStatus = carUseStatus()

const usageRef = ref('')
/* ====================列表功能==================== */
const queryData = reactive({
  current: 1,
  pageSize: 5,
  id: '',
  community: '',
  community_code: '',//小区名称
  parking_space: '', //车位名称
  license_plate: '', //车辆牌照
  owner: '', //车主姓名
  tel: '', //联系方式
  type: '',  //使用性质
  cost: '', //总费用
  create_time: '', //开始时间
  end_time: '', //结束时间
  remark: '', //备注
  begin_time: '', //创建时间
})

const total = ref(0)
const list = ref([])
const initData = async () => {
  let { data } = await getUsageList(queryData)
  // console.log(res);
  data.rows = data.rows.map(item => {
    item.create_time = dayjs(item.create_time).format('YYYY/MM/DD HH:mm:ss')
    item.end_time = dayjs(item.end_time).format('YYYY/MM/DD HH:mm:ss')
    item.begin_time = dayjs(item.begin_time).format('YYYY/MM/DD HH:mm:ss')
    return item
  })
  total.value = data.count
  list.value = data.rows
}
onMounted(() => {
  initData()
})

//搜索
const onSearch = () => {
  queryData.current = 1
  initData()
}

//重置
const onReset = () => {
  Object.assign(queryData, {
    current: 1,
    pageSize: 5,
    id: '',
    community: '',
    community_code: '',//小区名称
    parking_space: '', //车位名称
    license_plate: '', //车辆牌照
    owner: '', //车主姓名
    tel: '', //联系方式
    type: '',  //使用性质
    cost: '', //总费用
    create_time: '', //开始时间
    end_time: '', //结束时间
    remark: '', //备注
    begin_time: '', //创建时间
  })
  timeArr.value = ''
  initData()
}
//每页数量切换
const sizeChange = (val) => {
  queryData.pageSize = val
  initData()
}

//页码切换
const CurrentChange = val => {
  queryData.current = val
  initData()
}

//编辑修改显示
const editBtn = (id) => {
  usageRef.value.isShow = true
  usageRef.value.replyData(id)
  // console.log(usage.value);

}
//删除功能
const delRow = async (id) => {
  const res = await delUsageApi(id)
  console.log(res);
  if (!res.success) return ElMessage.error(res.msg)
  ElMessage.success('删除成功')
  if (queryData.current > 1 && list.value.length === 1) queryData.current--
  initData()
}


//处理时间
const timeArr = ref([])
watch(timeArr, (val) => {
  console.log(timeArr.value);
  if (timeArr.value) {
    queryData.create_time = val
      .map((item) => {
        item = +new Date(item)
        return item
      })
      .join(",")
    console.log(queryData.create_time);
  } else {
    queryData.create_time = ''
  }
})

</script>
<template>
  <div>
    <div class="app-container">
      <span style="font-weight: 700;">车位管理</span> <span>用于管理小区里每一个停车位的基本信息</span>
      <el-card>
        <!-- 搜索区域 -->
        <el-form label-width="80px" :inline="true" label-position="top">
          <el-form-item label="小区名称">
            <el-select clearable slot="prepend" placeholder="请选择" v-model="queryData.community_code">
              <el-option v-for="item in community" :key="item.id" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>

          <el-form-item label="车位名称">
            <el-input slot="prepend" placeholder="请输入" v-model="queryData.parking_space.name">
            </el-input>
          </el-form-item>

          <el-form-item label="车辆牌照">
            <el-input slot="prepend" placeholder="请输入" v-model="queryData.license_plate">
            </el-input>
          </el-form-item>

          <el-form-item label="车主姓名">
            <el-input slot="prepend" placeholder="请输入" v-model="queryData.owner">
            </el-input>
          </el-form-item>

          <el-form-item label="联系方式">
            <el-input slot="prepend" placeholder="请输入" v-model="queryData.tel">
            </el-input>
          </el-form-item>

          <el-form-item label="使用性质">
            <el-select clearable slot="prepend" placeholder="请选择" v-model="queryData.type">
              <el-option v-for="item in carStatus" :key="item.id" :label="item.type" :value="item.label" />
            </el-select>
          </el-form-item>

          <el-form-item label="创建时间">
            <el-date-picker v-model="timeArr" type="daterange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" clearable>
            </el-date-picker>
          </el-form-item>

          <el-form-item style="display: flex; align-items: end;">
            <div></div>
            <div>
              <el-button type="primary" @click="onSearch">搜索</el-button>
              <el-button @click="onReset">取消</el-button>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <div class="app-container">
      <el-card>
        <!-- 工具区域 -->
        <el-row type="flex" justify="space-between">
          <div></div>
          <div>
            <el-button type="primary" @click="usageRef.isShow = true">新增使用情况</el-button>
          </div>
        </el-row>
        <br>
        <!-- 表格区域 -->
        <el-table :data="list" border stripe>
          <el-table-column label="ID" prop="id" width="50px"></el-table-column>
          <el-table-column label="小区名称" prop="community.name"></el-table-column>
          <el-table-column label="车位名称" prop="parking_space.name"></el-table-column>
          <el-table-column label="车位牌照" prop="license_plate"></el-table-column>
          <el-table-column label="车主姓名" prop="owner"></el-table-column>
          <el-table-column label="联系方式" prop="tel" width="113px"></el-table-column>
          <el-table-column label="使用性质" prop="type">
            <template #default="{ row }">
              <span v-if="row.type == 1">售</span>
              <span v-else>租</span>
            </template>
          </el-table-column>
          <el-table-column label="总费用" prop="cost"></el-table-column>
          <el-table-column label="开始时间" prop="create_time" width="100px"></el-table-column>
          <el-table-column label="截止时间" prop="end_time" width="100px"></el-table-column>
          <el-table-column label="备注" prop="remark"></el-table-column>
          <el-table-column label="创建时间" prop="begin_time" width="100px"></el-table-column>
          <el-table-column label="操作" width="100px">
            <template #default="{ row }">
              <el-link class="link" type="primary" :underline="false" @click="editBtn(row.id)">编辑</el-link>
              <el-popconfirm title="确定删除吗？" @confirm="delRow(row.id)">
                <template #reference>
                  <el-link class="link" :underline="false" type="primary">删除</el-link>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <br>
        <!-- 分页区域 -->
        <el-row type="flex" justify="space-between"> 
          <div></div>
          <el-pagination v-model:current-page="queryData.current" v-model:page-size="queryData.pageSize"
          :page-sizes="[5, 10, 20]" :small="small" :disabled="disabled" :background="background"
          layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="sizeChange"
          @current-change="CurrentChange" />
        </el-row>
        <!-- 新增修改组件 -->
        <usage ref="usageRef" @updateList="initData" />
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.link {
  margin-right: 9px;
}
</style>